package StudentManager;

import domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

//二.首先完成用户界面的编写
public class StudentManager {
    public static void main(String[] args) {
        // 1. 搭建主界面菜单
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        fo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            int input = sc.nextInt();
            switch (input) {
                case 1:
                    //添加学生
                    AddStudent(list);
                    break;
                case 2:
                    //删除学生
                    DeletStudent(list);
                    break;
                case 3:
                    //修改学生
                    UpdateStudent(list);
                    break;
                case 4:
                    //查看学生
                    queryStudent(list);
                    break;
                case 5:
                    System.out.println("退出");
                    break fo;
                default:
                    System.out.println("您输入的选项不存在，请重新输入！");
            }
        }

    }

    private static void UpdateStudent(ArrayList<Student> list) {
        /*
        1.先判断学生是否存在
        2.如果存在进行修改
         */
        //输入需要删除的学生信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号：");
        String sid = sc.next();
        //使用index判断此学生是否存在
        int index = GetIndex(list,sid);
        //如果不存在输出提示后退出
        if(index == -1)
        {
            System.out.println("该学生不存在！");
            return;
        }
        //如果存在使用set函数进行修改
        System.out.println("请输入修改信息:");
        System.out.println("请输入修改后学生学号：");
        String id = sc.next();
        System.out.println("请输入修改后学生姓名：");
        String name = sc.next();
        System.out.println("请输入修改后学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入修改后学生生日：");
        String birthday = sc.next();
        Student newstu =new Student(id,name,age,birthday);
        list.set(index,newstu);
        System.out.println("修改成功！");
    }

    private static void DeletStudent(ArrayList<Student> list) {
        //输入被删学生的学号
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的学生学号：");
        String sid = sc.next();
        //判断这个学号是否存在
        int index = GetIndex(list,sid);
        //如果不存在输出提示后退出
        if(index == -1)
        {
            System.out.println("该学生不存在！");
            return;
        }
        //存在的话直接调用remove函数删除并提示
        list.remove(index);
        System.out.println("成功删除！");
    }

    private static void queryStudent(ArrayList<Student> list) {
        /*
        1.先判断是否为空集合
        2.如果不是空集合先打印表头信息
        3.打印学生信息
         */
        //1.先判断是否为空集合
        if(list.size()== 0){
            System.out.println("无信息！添加后重新查询！");
            return;
        }
        //2.如果不是空集合先打印表头信息
        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        //3.打印学生信息
        for(int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
        }

    }

    private static void AddStudent(ArrayList<Student> list) {
        String sid;
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入学生学号：");
            sid = sc.next();
            int index = GetIndex(list,sid);
            if(index ==-1)
                break;
        }
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu = new Student(sid, name,age,birthday);
        list.add(stu);
    }

    private static int GetIndex(ArrayList<Student> list,String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu =list.get(i);
            String temp = stu.getSid();
            if (temp.equals(sid)) {
                index = i;
            }
        }
        return index;
    }
}
